package com.gy.def.controller;

import com.gy.def.dataobject.GradeTable;
import com.gy.def.dataobject.MessageTable;
import com.gy.def.dataobject.UserTable;
import com.gy.def.enums.ResultEnum;
import com.gy.def.exception.GySchoolException;
import com.gy.def.service.MessageService;
import com.gy.def.utils.ResultVoUtil;
import com.gy.def.viewobject.PageMap;
import com.gy.def.viewobject.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

@CrossOrigin // 允许跨域
@Api(value = "MessageController", description = "消息模块")
@RestController
@RequestMapping("/message")
public class MessageController {

  @Autowired
  private MessageService messageService;

  @ApiOperation(value = "添加消息", notes = "添加消息")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "img", value = "消息内容图片", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "text", value = "消息内容", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "titleImg", value = "消息标题图片", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "messageSchool", value = "目标学校", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "messageTitle", value = "消息标题", required = true, dataType = "string")
  })
  @PostMapping("/addMessage")
  public ResultVo addMessage(@RequestParam("text") String text,
                             @RequestParam("messageTitle") String messageTitle) {
    MessageTable mt = new MessageTable();
    mt.setMessageText(text);
    mt.setMessageTitle(messageTitle);
    MessageTable result = messageService.save(mt);

    if (result == null) {
      throw new GySchoolException(ResultEnum.FAIL);
    }

    return ResultVoUtil.success();
  }

  @ApiOperation(value = "获取消息列表", notes = "获取消息列表")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "page", value = "第几页(小于0获取全部数据)", required = true, dataType = "string"),
    @ApiImplicitParam(paramType = "query", name = "pageSize", value = "一页有多少个数据", required = true, dataType = "string")
  })
  @PostMapping("/getMessageList")
  public ResultVo<PageMap<MessageTable>> getMessageList(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                                        @RequestParam(value = "pageSize", defaultValue = "0") Integer pageSize) {
    Page<MessageTable> mapList;
    Sort st = new Sort(Sort.Direction.DESC, "createTime");
    PageRequest pageRequest = new PageRequest(page, pageSize, st);
    mapList = messageService.findByIsDelete(0, pageRequest);

    PageMap<MessageTable> pm = new PageMap<>();
    pm.setNumber(mapList.getNumber());
    pm.setResultList(mapList.getContent());
    pm.setSize(mapList.getSize());
    pm.setTotalElements(mapList.getTotalElements());
    pm.setTotalPages(mapList.getTotalPages());
    return ResultVoUtil.success(pm);
  }

  @ApiOperation(value = "删除消息", notes = "删除消息")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "messageId", value = "消息id", required = true, dataType = "string")
  })
  @PostMapping("/deleteMessage")
  public ResultVo deleteMessage(@RequestParam("messageId") String messageId) {
    MessageTable mt = messageService.findByMessageId(messageId);
    if (mt == null) {
      return ResultVoUtil.error(ResultEnum.GetMessageNULL.getCode(), ResultEnum.GetMessageNULL.getMessage());
    }

    mt.setIsDelete(1);

    MessageTable result = messageService.save(mt);
    if (result == null) {
      throw new GySchoolException(ResultEnum.FAIL);
    }

    return ResultVoUtil.success();
  }
}
